package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"xdingtec.cn/xdkaika/app"
	"xdingtec.cn/xdkaika/app/http/api/service"
	form "xdingtec.cn/xdkaika/app/http/api/validator/card"
	"xdingtec.cn/xdkaika/app/models"
	"xdingtec.cn/xdkaika/utils"
	"xdingtec.cn/xdkaika/utils/response"
)

type CardController struct {
}

// @author      mz100
// @description 新增
// @param
// @return
func (CardController) Create(c *gin.Context) {
	var f form.CreateForm
	if err := c.ShouldBind(&f); err != nil {
		response.ValidatorError(c, err)
		return
	}
	ctms := &service.CardTemplateService{}
	ctm, err := ctms.Get(uint(f.CardTemplateId))
	if err != nil {
		response.Fail(c, 400001, "查询卡模板失败", err.Error())
		return
	}
	m := &models.CardModel{
		UUID:           uuid.New(),
		CreateByUserId: app.JWTClaims.UserId,
		Customer:       f.Customer,
		Mobile:         f.Mobile,
		StartAt:        utils.GetTimeParseDate(f.StartAt),
		ExpiresAt:      utils.GetTimeParseDate(f.ExpiresAt),
		CardTemplateId: uint(f.CardTemplateId),
		Count:          ctm.Count,
	}
	ms := &service.CardService{}
	res, err := ms.Create(m)
	if err != nil || res == nil {
		response.ErrorSystem(c, "新增卡失败", gin.H{"err": err.Error()})
		return
	}
	response.Success(c, "新增卡成功", res)
}

// @author      mz100
// @description 获取管理卡片分页数据
// @param
// @return
func (CardController) GetPage(c *gin.Context) {
	var f form.PageForm
	if err := c.ShouldBindQuery(&f); err != nil {
		response.ValidatorError(c, err)
		return
	}
	f.CustomerId = int(app.JWTClaims.UserId)
	ms := &service.CardService{}
	p, err := ms.GetPage(&f)
	if err != nil {
		response.ErrorSystem(c, "获取列表失败", gin.H{"err": err.Error()})
		return
	}
	response.Success(c, "", p)
}

// @author      mz100
// @description 获取管理卡片分页数据
// @param
// @return
func (CardController) GetManagePage(c *gin.Context) {
	var f form.PageForm
	if err := c.ShouldBindQuery(&f); err != nil {
		response.ValidatorError(c, err)
		return
	}
	f.CreateByUserId = int(app.JWTClaims.UserId)
	ms := &service.CardService{}
	p, err := ms.GetPage(&f)
	if err != nil {
		response.ErrorSystem(c, "获取列表失败", gin.H{"err": err.Error()})
		return
	}
	response.Success(c, "", p)
}

// @time        2022/05/10
// @author      mz100
// @description 卡片标注（打卡
// @param
// @return
func (CardController) Mark(c *gin.Context) {
	var f form.MarkForm
	if err := c.ShouldBind(&f); err != nil {
		response.ValidatorError(c, err)
		return
	}
	ms := &service.CardService{}
	res, err := ms.Mark(&f)
	if err != nil || !res {
		response.ErrorSystem(c, "打卡失败", gin.H{"err": err.Error()})
		return
	}
	response.Success(c, "", gin.H{})
}

// @time        2022/05/10
// @author      mz100
// @description 获取卡片详情
// @param
// @return
func (CardController) GetDetail(c *gin.Context) {
	var f form.DetailForm
	if err := c.ShouldBind(&f); err != nil {
		response.ValidatorError(c, err)
		return
	}
	ms := &service.CardService{}
	res, err := ms.GetDetail(&f)

	if err != nil {
		response.ErrorSystem(c, "获取卡片失败", gin.H{"err": err.Error()})
		return
	}
	response.Success(c, "", res)

}
